package router

import (
	"WchimeGinSystem/api/v1/system"
	"WchimeGinSystem/conf"
	"WchimeGinSystem/docs"
	"WchimeGinSystem/middleware"

	"github.com/gin-gonic/gin"
	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func InitRouter() {
	gin.SetMode(conf.MODE)

	r := gin.Default()
	_ = r.SetTrustedProxies(nil)
	r.Use(gin.Recovery())

	// swagger
	registerSwagger(r)
	// 系统接口
	registerSystemUrl(r)

	_ = r.Run(conf.RUNPORT)

}

func registerSwagger(r gin.IRouter) {
	// API文档访问地址: http://host/swagger/index.html
	// 注解定义可参考 https://github.com/swaggo/swag#declarative-comments-format
	// 样例 https://github.com/swaggo/swag/blob/master/example/basic/api/api.go
	docs.SwaggerInfo.BasePath = "/v1"
	docs.SwaggerInfo.Title = "接口文档"
	docs.SwaggerInfo.Description = "接口文档"
	docs.SwaggerInfo.Version = "1.0"
	// docs.SwaggerInfo.Host = "localhost:8080"
	docs.SwaggerInfo.Schemes = []string{"http", "https"}

	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
}

func registerSystemUrl(r gin.IRouter) {
	/*
		系统接口
	*/
	// 不需要验证
	system_url_open := r.Group("v1")
	{

		system_url_open.POST("login", system.Login)
		system_url_open.GET("get_img", system.GetImg)

	}
	// 需要验证身份
	system_url_auth := r.Group("v1")
	system_url_auth.Use(middleware.JWTAuth())
	{
		system_url_auth.GET("userinfo", system.Userinfo)
		system_url_auth.POST("update_avatar", system.UpdateAvatar)
		system_url_auth.POST("update_pwd", system.UserModifyPWD)
		system_url_auth.PUT("userinfo", system.UpdateUserinfo)

	}
	// 需要验证管理员身份
	system_url_admin := r.Group("v1")
	system_url_admin.Use(middleware.JWTAuth())
	system_url_admin.Use(middleware.SuperAdminAuth())
	{
		system_url_admin.POST("create_user", system.CreateUser)

	}
}
